//
//  WiFiSendDataManager.m
//  SmartYunYiWindow
//
//  Created by JianWei Chen on 17/4/10.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "WiFiSendDataManager.h"

#define SendDataTime 0.1       //指令发送间隔时间长度



@interface WiFiSendDataManager ()

@property (strong, nonatomic) NSDate *currentDate;
@property (strong, nonatomic) NSTimer *actionTimer;
@property (strong, nonatomic) NSMutableArray *actionArray;

@end

@implementation WiFiSendDataManager
#pragma mark - 初始化
+ (WiFiSendDataManager *)defaultService
{
    static WiFiSendDataManager *static_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        static_manager = [[WiFiSendDataManager alloc]init];
    });
    return static_manager;
}

- (instancetype)init
{
    if ([super init]) {
        [self actionTimer];
    }
    return self;
}

- (NSMutableArray *)actionArray
{
    if (!_actionArray) {
        _actionArray = [[NSMutableArray alloc]init];
    }
    return _actionArray;
}

- (NSTimer *)actionTimer
{
    if (!_actionTimer) {
        _actionTimer = [NSTimer scheduledTimerWithTimeInterval:SendDataTime target:self selector:@selector(doAction:) userInfo:nil repeats:YES];
    }
    return _actionTimer;
}

#pragma mark - 事件处理
#pragma mark - 按时间间隔执行事件
- (void)addCirculateDoAction:(WiFiToothAction)action
{
    [self.actionArray addObject:action];
}



- (void)doAction:(NSTimer *)timer
{
    if (self.actionArray.count != 0) {
        WiFiToothAction action = self.actionArray[0];
        action();
        [self.actionArray removeObject:action];
    }
}

- (void)sendDataByCode:(NSString *)code command:(NSString *)command mac:(NSString *)macAddress
{
    static  int i = 0;
    WiFiToothAction block = ^{
        i++;
        int status = [[DTCloudManager defaultJNI_iOS_SDK] sendCommandByFunctionCode:code functionCommand:command deviceMacAddress:macAddress deviceType:DeviceUsedByWiFi protocolType:DTSDKProtocolTypeThird];
        if (status != 0) {
            NSLog(@"%@ : %@  发送失败，%@",code,command,macAddress);
        }
    };
    [self addCirculateDoAction:block];
    
}

- (NSString *)updateCommandLeng:(NSString *)command
{
    if (command.length < 30) {
        for (int i = (int)command.length; i < 30; i++) {
            command = [NSString stringWithFormat:@"%@0",command];
        }
    }
    return command;
}
#pragma mark - 指令写在下方
- (void)getDeviceState:(NSString *)macAddress {
    NSString *functionCode = @(AppToDeviceCommandDeviceState).hexString;
    NSString *command = [self updateCommandLeng:@""];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingTiming:(NSString *)macAddress value:(NSString *)value{
    NSString *functionCode = @(AppToDeviceCommandDeviceTiming).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingTemperature:(NSString *)macAddress value:(NSString *)value {
    NSString *functionCode = @(AppToDeviceCommandDeviceTemperature).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingSleep:(NSString *)macAddress value:(NSString *)value {
    NSString *functionCode = @(AppToDeviceCommandDeviceSleep).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingLock:(NSString *)macAddress value:(NSString *)value {
    NSString *functionCode = @(AppToDeviceCommandDeviceLock).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingLight:(NSString *)macAddress value:(NSString *)value {
    NSString *functionCode = @(AppToDeviceCommandDeviceLight).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}

- (void)settingPower:(NSString *)macAddress value:(NSString *)value {
    NSString *functionCode = @(AppToDeviceCommandDevicePower).hexString;
    NSString *command = [self updateCommandLeng:value];
    [self sendDataByCode:functionCode command:command mac:macAddress];
}




@end
